<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="产线/产线组" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="line">
              <j-dict-select-tag type="list" v-model="model.line" dictCode="mes_line_cfg,LINE,LINE"
                                 placeholder="请选择产线/产线组" @change="lineChange" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="产品/产品组" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="partno">
              <a-radio-group placeholder="请选择产品/产品组" v-model="model.partno">
                <a-radio :value="partNo">
                  产品
                </a-radio>
                <a-radio :value="pnGroup">
                  产品组
                </a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="路由名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="routename">
              <a-select placeholder="请选择路由名称" v-model="model.routename">
                <a-select-option v-for="d in routeList" :key="d" :value="d">
                  {{ d }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="顺序号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sequence">
              <a-input-number v-model="model.sequence" placeholder="请输入顺序号" style="width: 100%"></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="list" v-model="model.status" dictCode="isEnabled"
                                 placeholder="请选择状态" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>

import { getAction, httpAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'MesLinePartNoCfgModal',
  components: {},
  props: {
    partNo: {
      type: String,
      required: false,
      default: ''
    },
    pnGroup: {
      type: String,
      required: false,
      default: ''
    }
  },
  data() {
    return {
      isEdit: false,
      title: '操作',
      width: 800,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      validatorRules: {
        itempartno: [
          { required: true, message: '请输入物料!' }
        ],
        qtyPer: [
          { required: true, message: '请输入消耗数量!' }
        ],
        op: [
          { required: true, message: '请输入需要上料的工序!' }
        ],
        lottrack: [
          { required: true, message: '请输入是否需要扣减物料批次!' }
        ]
      },
      url: {
        routeList: '/basic/mesRoutingCfg/list',
        add: '/basic/mesLinePartnoCfg/add',
        edit: '/basic/mesLinePartnoCfg/edit'
      },
      routeList: []

    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    lineChange() {
      console.log('产线改变')
      this.getRouteList()
    },
    getRouteList() {
      getAction(this.url.routeList, { line: this.model.line }).then(res => {
        console.log('返回值')
        console.log(res)
        let newList2 = []
        res.result.records.forEach((item) => {
          if (!newList2.includes(item.routename)) {
            newList2.push(item.routename)
          }
        })
        this.routeList = newList2
      })
    },
    add() {
      this.isEdit = false
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      if (JSON.stringify(record) !== '{}') {
        this.isEdit = true
      }
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.$refs.form.clearValidate()
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.isEdit) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    }

  }
}
</script>
